//
//  BindAlipayViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/9/2.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "UIButton+CountDown.h"

#import "BindAlipayViewController.h"

@interface BindAlipayViewController ()

@property (weak, nonatomic) IBOutlet UITextField *nameTF;

@property (weak, nonatomic) IBOutlet UITextField *alipayCountTF;

@property (weak, nonatomic) IBOutlet UITextField *codeTF;

@property (weak, nonatomic) IBOutlet UIButton *countDownBtn;



@end

@implementation BindAlipayViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)initView {
    self.title = @"绑定支付宝";
    
    self.nameTF.text = [TTUserModel sharedObject].realName;
    self.alipayCountTF.text = [TTUserModel sharedObject].alipayId;
    
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitleColor:UIColorHex(0xff434e) forState:UIControlStateNormal];
    [rightBtn setTitle:@"绑定" forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(bingAlipay) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    rightBtn.frame = CGRectMake(0,0,40,30);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];

    self.navigationItem.rightBarButtonItems =@[rightItem];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.countDownBtn pp_clipCorners:UIRectCornerAllCorners radius:self.countDownBtn.frame.size.height/2];
    });
}

- (IBAction)codeBtnAction:(id)sender {
    [self request_bingSms];
}

- (void)bingAlipay {
    
    if (self.nameTF.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入姓名"];
        [SVProgressHUD dismissWithDelay:1];
        return;
    }
    
    if (self.alipayCountTF.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入支付宝账号"];
        [SVProgressHUD dismissWithDelay:1];
        return;
    }
    
    if (self.codeTF.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
        [SVProgressHUD dismissWithDelay:1];
        return;
    }
    
    [self request_updateUserInfo];
}

- (void)request_updateUserInfo {
    [self bindAliPay];
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.nameTF.text forKey:@"realName"];
    [dic setValue:self.alipayCountTF.text forKey:@"alipayId"];
    [dic setValue:self.codeTF.text forKey:@"vcode"];
    [dic setValue:[TTUserModel sharedObject].username forKey:@"username"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_EditMyInfo];
    WEAKSELF
    //[SVProgressHUD showWithStatus:@"正在绑定"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                [[TTUserModel sharedObject].userModelDefault setValue:self.nameTF.text forKey:@"TT_realName"];
                [[TTUserModel sharedObject].userModelDefault setValue:self.alipayCountTF.text forKey:@"TT_alipayId"];
                //[SVProgressHUD showSuccessWithStatus:[data valueForKey:@"message"]];
                //[SVProgressHUD dismissWithDelay:1];
                //[weakSelf.navigationController popViewControllerAnimated:YES];
            } else {
                //[SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                //[SVProgressHUD dismissWithDelay:1];
            }
        }
    } FailedBlock:^(NSError * _Nonnull error) {
        //[SVProgressHUD dismiss];
    }];
}
- (void)bindAliPay {
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    [dic setValue:@"ali" forKey:@"bindType"];
    [dic setValue:self.alipayCountTF.text forKey:@"aliId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_BindWXAli];
    [SVProgressHUD showWithStatus:@"正在绑定"];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                [SVProgressHUD showSuccessWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
                [weakSelf.navigationController popViewControllerAnimated:YES];
                !weakSelf.refresh ?: weakSelf.refresh();
            } else {
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}

- (void)request_bingSms {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:[TTUserModel sharedObject].username forKey:@"mobile"];
    [dic setValue:@"7" forKey:@"type"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_SMS];

    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        [SVProgressHUD dismiss];

        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                
                [weakSelf.countDownBtn startWithTime:59
                                           title:@"重新获取"
                                  countDownTitle:@"s"
                                       mainColor:UIColorHex(0xFDB44B)
                                      countColor:UIColorHex(0xFDB44B)];
            } else {
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {

        [SVProgressHUD showErrorWithStatus:@"请求异常"];
        [SVProgressHUD dismissWithDelay:1];

    }];
}

@end
